<template>
  <div style="margin: 0 10px;">
    <div style="float:right;margin:5px 0px 5px 5px">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd">新增</el-button>
        <el-button type="success" plain icon="el-icon-refresh" size="mini" @click="getList">刷新</el-button>
      </el-col>
    </div>

    <el-table v-loading="loading" :data="functionList" border>
      <el-table-column label="隶属功能" width="80px" align="center" prop="ctFunP" />
      <el-table-column label="子功能号" width="80px" align="center" prop="ctFunNo" />
      <el-table-column label="对应功能号" width="100px" align="center" prop="ctChildFunNo" />
      <el-table-column label="子功能名称" align="center" prop="ctFunName" />
      <el-table-column label="对应表" align="center" prop="ctTable" />
      <el-table-column label="路由地址" align="center" prop="ctUrl" />
      <el-table-column label="标题图标" width="120px" align="center" prop="ctWinIcon" />
      <el-table-column label="强制刷新" width="120px" align="center" prop="ctIfShowretrieve" />
      <el-table-column label="有效标志" width="120px" align="center" prop="ctState" />
      <el-table-column
        label="操作"
        width="120px"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="medium"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            title="修改"
          ></el-button>
          <!--          <el-button-->
          <!--              size="medium"-->
          <!--              type="text"-->
          <!--              icon="el-icon-delete"-->
          <!--              @click="handleDelete(scope.row)"-->
          <!--              title="删除"-->
          <!--          >-->
          <!--          </el-button>-->
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改功能列表对话框  -->
    <el-dialog :title="title" :visible.sync="open" width="700px" append-to-body>
      <el-row class="mb8">
        <div style="float:right;">
          <el-button type="primary" size="mini" @click="submitForm" v-if="canSave">保 存</el-button>
        </div>
      </el-row>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px" class="main-form">
        <el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12">
          <el-form-item label="隶属功能" prop="ctFunP">
            <mis-select-tree
              :mis-tree-type="901"
              :select-multiple="false"
              :disable-branch-nodes="true"
              @updateSelectTreeVal="updateSelectTreeValctFunP"
              :setValue="form.ctFunP"
              :disabled="true"
            ></mis-select-tree>
          </el-form-item>
        </el-col>
        <el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12">
          <el-form-item label="功能名称" prop="ctFunName">
            <el-input v-model="form.ctFunName" placeholder="请输入功能名称" />
          </el-form-item>
        </el-col>
        <el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12">
          <el-form-item label="功能编号" prop="ctFunNo">
            <el-input v-model="form.ctFunNo" placeholder="请输入功能编号" title="注意编号，一般从0开始" />
          </el-form-item>
        </el-col>
        <el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12">
          <el-form-item label="对应表" prop="ctTable">
            <el-input v-model="form.ctTable" placeholder="请输入对应表" />
          </el-form-item>
        </el-col>
        <el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12">
          <el-form-item label="路由地址" prop="ctUrl">
            <el-input v-model="form.ctUrl" placeholder="请输入详情页面路由地址" />
          </el-form-item>
        </el-col>
        <el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12">
          <el-form-item label="对应功能号" prop="ctChildFunNo">
            <el-input v-model="form.ctChildFunNo" placeholder="请输入对应功能号" />
          </el-form-item>
        </el-col>
        <el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12">
          <el-form-item label="标题图标" prop="ctWinIcon">
            <el-popover
              placement="bottom-start"
              width="460"
              trigger="click"
              @show="$refs['iconSelect'].reset()"
            >
              <IconSelect ref="iconSelect" @selected="selected" />
              <el-input slot="reference" v-model="form.ctWinIcon" placeholder="点击选择图标" readonly>
                <svg-icon
                  v-if="form.ctWinIcon"
                  slot="prefix"
                  :icon-class="form.ctWinIcon"
                  class="el-input__icon"
                  style="height: 32px;width: 16px;"
                />
                <i v-else slot="prefix" class="el-icon-search el-input__icon" />
              </el-input>
            </el-popover>
          </el-form-item>
        </el-col>
        <el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12">
          <el-form-item label="强制刷新" prop="ctIfShowretrieve">
            <el-switch active-value="1" inactive-value="0" v-model="form.ctIfShowretrieve"></el-switch>
          </el-form-item>
        </el-col>
        <el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12">
          <el-form-item label="有效标志" prop="ctState">
            <el-switch active-value="1" inactive-value="9" v-model="form.ctState"></el-switch>
          </el-form-item>
        </el-col>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
import {
  listFunction,
  getFunction,
  delFunction,
  addFunction,
  updateFunction,
  exportFunction,
} from '@/api/mis/function'
import MisSelectTree from '@/components/SpsPublic/treeview/MisSelectTree'
import IconSelect from '@/components/IconSelect'
import store from '@/store/index'
import { getFunNo } from '@/utils/party'

export default {
  name: 'subFunction',
  components: {
    MisSelectTree,
    IconSelect,
  },
  data() {
    return {
      // 判断是新增还是修改
      ifadd: false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 功能列表表格数据
      functionList: [],
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {},
    }
  },
  created() {
    this.reset()
    this.getList()
  },
  methods: {
    // 选择图标
    selected(name) {
      this.form.ctWinIcon = name
    },
    /** 查询功能列表列表 */
    getList() {
      this.loading = true
      this.queryParams.ctFunP = this.subFunNo
      listFunction(this.queryParams)
        .then((response) => {
          this.functionList = response.rows
          this.total = response.total
          this.loading = false
        })
        .catch(() => {
          this.loading = false
        })
    },

    // 表单重置
    reset() {
      this.form = {
        ctFunNo: null,
        ctFunP: null,
        ctFunName: null,
        ctFunType: null,
        ctTable: null,
        ctView: null,
        ctBussType: null,
        ctUrl: null,
        ctWinModel: null,
        ctWinWidth: null,
        ctCodeIso: null,
        ctOrderby: null,
        ctTableMainid: null,
        ctTableChild: null,
        amRow: null,
        ctFlowNo: null,
        ctFilterUser: null,
        ctFilterBusstype: null,
        ctFilterOrg: null,
        ctFilterStep: null,
        ctFilterOther: null,
        ctIfCheckUser: null,
        ctIfFile: null,
        ctIfSubmit: null,
        ctIfSign: null,
        ctIfReview: null,
        ctIfShowretrieve: null,
        ctWinIcon: null,
        ctIfStep: null,
        ctState: null,
      }
      this.resetForm('form')
    },

    /** 新增按钮操作 */
    handleAdd() {
      this.open = true
      this.title = '添加子功能'
      this.ifadd = true

      const promise = new Promise((resolve, reject) => {
        this.reset()
        resolve()
      })
      promise.then(() => {
        this.form.ctFunP = this.subFunNo
        this.form.ctFunNo = this.subFunNo + '_'
        this.form.ctState = '1'
        this.form.ctFunType = '2'
      })
      // this.reset()
      // this.$nextTick(() => {
      //   this.form.ctFunP = this.subFunNo
      //   this.form.ctFunNo = this.subFunNo + '_'
      //   this.form.ctState ='1'
      // })
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset()
      this.ifadd = false
      const ctFunNo = row.ctFunNo || this.ids
      getFunction(ctFunNo).then((response) => {
        this.form = response.data
        this.open = true
        this.title = '修改子功能'
      })
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs['form'].validate((valid) => {
        if (valid) {
          if (!this.ifadd) {
            updateFunction(this.form).then((response) => {
              this.msgSuccess('修改成功')
              this.open = false
              this.getList()
            })
          } else {
            addFunction(this.form).then((response) => {
              this.msgSuccess('新增成功')
              this.open = false
              this.getList()
            })
          }
        }
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ctFunNos = row.ctFunNo || this.ids
      this.$confirm(
        '是否确认删除功能列表编号为"' + ctFunNos + '"的数据项?',
        '警告',
        {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning',
        }
      )
        .then(function () {
          return delFunction(ctFunNos)
        })
        .then(() => {
          this.getList()
          this.msgSuccess('删除成功')
        })
    },

    updateSelectTreeValctFunP(value) {
      this.form.ctFunP = value
    },
  },
}
</script>
<style>
.main-form .vue-treeselect__control {
  width: 100%;
}
</style>
